package com.guoning.office_system.controller;

import com.alibaba.fastjson.JSONArray;
import com.guoning.office_system.model.Message;
import com.guoning.office_system.service.MessageService;
import com.guoning.office_system.utils.Constants;
import com.guoning.office_system.utils.JsonResult;
import com.guoning.office_system.vo.Criteria;
import com.guoning.office_system.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("message")
public class MessageController {

    @Autowired
    private MessageService messageService;


    @RequestMapping(value = "/selectByQueryCondition")
    public JsonResult selectByQueryCondition(@RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex,
                                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                             @RequestParam(value = "sendUserId", defaultValue = "") Long sendUserId,
                                             @RequestParam(value = "acceptUserId", defaultValue = "") Long acceptUserId,
                                             @RequestParam(value = "enableStatus", defaultValue = "") Integer enableStatus,
                                             @RequestParam(value = "messageStatus", defaultValue = "") Integer messageStatus,
                                             @RequestParam(value = "messageCodeOrContent", defaultValue = "") String messageCodeOrContent){
        JsonResult js;
        try {
            Criteria criteria = new Criteria();
            PageBean pageBean = new PageBean();
            criteria.addExtField("sendUserId", sendUserId);
            criteria.addExtField("acceptUserId", acceptUserId);
            criteria.addExtField("enableStatus", enableStatus);
            criteria.addExtField("messageStatus", messageStatus);
            criteria.addExtField("messageCodeOrContent", messageCodeOrContent);
            pageBean.setPageIndex(pageIndex);
            pageBean.setPageSize(pageSize);
            pageBean = messageService.selectByQueryCondition(criteria, pageBean);
            js = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        }catch (Exception e){
            e.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return js;
    }

    /**
     * 全部标为已读
     * @param userId
     * @return
     */
    @RequestMapping(value = "/haveAllRead", method = RequestMethod.POST)
    public JsonResult haveAllRead(Long userId){
        JsonResult js;
        try {
            int i = messageService.updateAllMessageByUserId(userId);
            js = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return js;
    }

    /**
     * 标为已读
     * @param params
     * @return
     */
    @RequestMapping(value = "/haveRead", method = RequestMethod.POST)
    public JsonResult haveRead(String params){
        JsonResult js;
        try {
            List<Message> list = JSONArray.parseArray(params, Message.class);
            int i = messageService.updateHaveReadById(list);
            js = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return js;
    }

    /**
     * 进入回收站
     * @param params
     * @return
     */
    @RequestMapping(value = "/delMessage", method = RequestMethod.POST)
    public JsonResult delMessage(String params){
        JsonResult js;
        try {
            List<Message> list = JSONArray.parseArray(params, Message.class);
            int i = messageService.updateDeleteById(list);
            js = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return js;
    }

    /**
     * 撤销回收站
     * @param params
     * @return
     */
    @RequestMapping(value = "/recoverMessage", method = RequestMethod.POST)
    public JsonResult recoverMessage(String params){
        JsonResult js;
        try {
            List<Message> list = JSONArray.parseArray(params, Message.class);
            int i = messageService.updateRecoverById(list);
            js = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception e){
            js = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return js;
    }

    /**
     * 获取草稿
     * @param userId
     * @return
     */
    @RequestMapping(value = "/draft")
    public JsonResult draft(Long userId){
        JsonResult js;
        try {
            Message message = messageService.selectDraftByUserId(userId);
            if(message == null){
                js = new JsonResult(Constants.STATUS_NOT_FOUND, "草稿不存在");
            }else {
                js = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", message);
            }
        }catch (Exception e){
            e.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return js;
    }

    /**
     * 新增草稿
     * @param message
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult insert(Message message){
        JsonResult js;
        try {
            int i = messageService.insert(message);
            js = new JsonResult(Constants.STATUS_SUCCESS, "保存成功", message);
        }catch (Exception e){
            e.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "保存异常");
        }
        return js;
    }

    /**
     * 发送消息
     * @param message
     * @return
     */
    @RequestMapping(value = "/insertSelective", method = RequestMethod.POST)
    public JsonResult insertSelective(Message message){
        JsonResult js;
        try {
            Message message1 = messageService.selectDraftByUserId(message.getSendUserId());
            if(message1 != null){
                message.setId(message1.getId());
                messageService.updateByPrimaryKeySelective(message);
            }else {
                int i = messageService.insertSelective(message);
            }
            js = new JsonResult(Constants.STATUS_SUCCESS, "发送成功");
        }catch (Exception e){
            e.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "发送异常");
        }
        return js;
    }

}
